//
//  PicPassController.m
//  FinancialProduct
//
//  Created by Stella on 2017/8/24.
//  Copyright © 2017年 maq. All rights reserved.
//
#import "AppDelegate.h"
#import "ZSBntview.h"
#import "UserModel.h"
#import "MyNavigationController.h"
#import "CKPuslichTool.h"
#import "HandleNetLogin.h"
#import "PicPassController.h"
#import "LoginViewController.h"
#import "ReSetPassController2.h"
#import "RegisterTextController.h"
#import "DBAcquireAuthCodeButton.h"

@interface PicPassController ()<ZSBntviewDelegate>{
    int count;
    int num;
}
@property (weak, nonatomic) IBOutlet DBAcquireAuthCodeButton *codeButton;
@property (weak, nonatomic) IBOutlet UILabel *titleLa;
@property (weak, nonatomic) IBOutlet UILabel *bottomLabel;
@property (weak, nonatomic) IBOutlet ZSBntview *BtnView;
@property (weak, nonatomic) IBOutlet UILabel *reminderLabel;
@property (weak, nonatomic) IBOutlet UIImageView *topImage;
@property (strong, nonatomic)NSString *passStr;
@property (weak, nonatomic) IBOutlet UIButton *reset;
@property (weak, nonatomic) IBOutlet UIButton *skipButton;
@property (weak, nonatomic) IBOutlet UIButton *forgetButton;
@property (weak, nonatomic) IBOutlet UIButton *loginOther;
@property (weak, nonatomic) IBOutlet UILabel *line1;
@property (weak, nonatomic) IBOutlet UILabel *line2;
@property (weak, nonatomic) IBOutlet UILabel *welcomeLa;

//忘记手势时验证用
@property (weak, nonatomic) IBOutlet UIView *resetView;
@property (weak, nonatomic) IBOutlet UITextField *CodeTF;
@property (strong,nonatomic)NSDate *oldDate;
@property (weak, nonatomic) IBOutlet UILabel *reminder1;
@property (weak, nonatomic) IBOutlet UIButton *checkBt;
@property (strong,nonatomic)NSString *mCode;
@end

@implementation PicPassController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
//    [self loadMyView];
}
-(void)loadMyView{
    count=0;
    num=0;
    _passStr=@"";
    self.reminderLabel.text=@"请绘制您的手势密码";
    self.reset.hidden=YES;
    UIViewSetRadius(self.reset, 5, 0, nil);
    UIViewSetRadius(self.checkBt, 5, 0, nil);
    UIViewSetRadius(self.CodeTF, 5, 0, nil);
    UIViewSetRadius(self.codeButton, 5, 0, nil);
    self.BtnView.delegate = self;
    self.topImage.image=[UIImage imageNamed:@"momo.png"];
    
    
    NSString *oldPass=[[NSUserDefaults standardUserDefaults]objectForKey:@"PICPASSCODE"];
//    NSLog(@"oldPass==%@",oldPass);
    if (oldPass.length>3) {
        //登录检查
        
        self.topImage.image=[UIImage imageNamed:@""];
        self.skipButton.hidden=YES;
        self.titleLa.hidden=YES;
        self.bottomLabel.hidden=YES;
        self.forgetButton.hidden=NO;
        self.loginOther.hidden=NO;
        self.line1.hidden=NO;
        self.line2.hidden=NO;
        self.welcomeLa.hidden=NO;
    }else{
        //设置密码
        self.skipButton.hidden=NO;
     
        self.titleLa.hidden=NO;
        self.bottomLabel.hidden=NO;
        self.forgetButton.hidden=YES;
        self.loginOther.hidden=YES;
        self.line1.hidden=YES;
        self.line2.hidden=YES;
        self.welcomeLa.hidden=YES;
    }


}

- (IBAction)goLoginView:(id)sender {
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"PICPASSCODE"];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"USERISLOGINKEY"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    BOOL hasLogin=NO;
    for (UIViewController *vc in self.navigationController.viewControllers) {
        if ([vc isKindOfClass:[LoginViewController class]]) {
            hasLogin=YES;
            [self.navigationController popToViewController:vc animated:NO];
            break;
        }
    }
    if (!hasLogin) {
        LoginViewController *vcc=(LoginViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"LoginViewController"];
        [self.navigationController pushViewController:vcc animated:YES];
    }
    
//    LoginViewController *Login = (LoginViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"LoginViewController"];
//    AppDelegate *tempAppDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    MyNavigationController *nav = [[MyNavigationController alloc]initWithRootViewController:Login];
//    tempAppDelegate.window.rootViewController=nav;
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadMyView];
}
//实现代理方法
-(void)zsbntview:(ZSBntview *)bntview moveResult:(NSString *)strM{
    
    
    
    //开启一个图形上下文
    UIGraphicsBeginImageContextWithOptions(bntview.frame.size, NO, 0.0);
    //获取图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //截图
    [self.BtnView.layer renderInContext:ctx];
    //获取图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    //关闭图形上下文
    UIGraphicsEndImageContext();
    //把获取的图片保存到 imageview 中
    self.topImage.image = image;
    NSString *oldPass=[[NSUserDefaults standardUserDefaults]objectForKey:@"PICPASSCODE"];
    if (oldPass.length>3) {
        
        //登录检查
        if (![oldPass isEqualToString:strM]) {
            num++;
            if (5-num==0) {
                
                [self goLoginView:nil];
                return;
            }
            NSString *textSt=[NSString stringWithFormat:@"手势密码错误,剩余%d次",5-num];
            self.reminderLabel.text=textSt;
        }else{
            [self goHomeController];
        }
    }else{
        [self setPicPassWith:strM];
    }

//    NSLog(@"strMstrM===%@",strM);
}
- (IBAction)skip:(id)sender {
    [self goHomeController];
}
-(void)goHomeController{
    [[NSUserDefaults standardUserDefaults]setObject:@"YES" forKey:@"showQY"];
    AppDelegate *tempAppDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    MyTabBarController *my=(MyTabBarController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Main" viewID:@"MyTabBarController"];
    tempAppDelegate.window.rootViewController = my;
    UserModel *mode=[UserModel getUser];
    if ([mode.Accountstatus intValue]==1) {
        NSDictionary *dic=@{@"index":@"2"};
        [[NSNotificationCenter defaultCenter]postNotificationName:@"goToOtherView" object:nil userInfo:dic];
    }
    
    
}
- (IBAction)forgetMyPicPass:(id)sender {
    
//    RegisterTextController *vc=(RegisterTextController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"RegisterTextController"];
//    vc.returnAgree = ^{
////        self.AgreeBt.selected=YES;
//    };
//    [self.navigationController pushViewController:vc animated:YES];
    
    ReSetPassController2 *vc=(ReSetPassController2 *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"ReSetPassController2"];
    vc.whereFrom=@"reSetPIC";
    UserModel *mode=[UserModel getUser];
    NSDictionary *info=@{@"name":mode.CustomerName,@"phone":mode.Phone};
    vc.userinfo=info;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)setPicPassWith:(NSString *)passnew{
    //设置密码
    if (count==0 && [_passStr isEqualToString:@""]) {
        if (passnew.length<4) {
            self.reminderLabel.text=@"手势密码请最少绘制4个点";
            return;
        }
        _passStr=passnew;
        count++;
        self.reminderLabel.text=@"请再次绘制进行确认";
        self.reset.hidden=NO;
        
    }else if (count==1){
        if (![_passStr isEqualToString:passnew]) {
            self.reminderLabel.text=@"您两次绘制的不一致，请重新绘制";
            count=0;
            _passStr=@"";
        }else{
            [[NSUserDefaults standardUserDefaults]setObject:_passStr forKey:@"PICPASSCODE"];
            
            [self goHomeController];
        }
    }

}
- (IBAction)resetpicpasss:(id)sender {
    self.reminderLabel.text=@"请绘制您的手势密码";
    count=0;
    _passStr=@"";
    self.reset.hidden=YES;
    self.topImage.image = [UIImage imageNamed:@"momo"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
